package com.finer.service.goods.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.finer.dao.goods.WarehouseGoodsDao;
import com.finer.dto.warehouse.GoodsPageDTO;
import com.finer.entity.ResponseMessage;
import com.finer.entity.document.FinerGoods;
import com.finer.service.goods.WarehouseGoodsService;
import com.finer.utils.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wangenhao
 * @className WarehouseGoodsServiceImpl
 * @description 商品service实现类
 * @date 2024-10-22 下午2:18
 **/
@Service
@Slf4j
public class WarehouseGoodsServiceImpl implements WarehouseGoodsService {

    @Autowired
    private WarehouseGoodsDao warehouseGoodsDao;

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 查询商品分页
     * @date 2024/10/22 下午6:48
     * @Param goodsPageDTO:
     **/
    public ResponseMessage getGoodsPage(GoodsPageDTO goodsPageDTO) {
        int pageNum = 1;
        int pageSize = 10;
        if (goodsPageDTO.getPageNum() != null) {
            pageNum = goodsPageDTO.getPageNum();
        }
        if (goodsPageDTO.getPageSize() != null) {
            pageSize = goodsPageDTO.getPageSize();
        }
        Page<FinerGoods> page = PageHelper.startPage(pageNum, pageSize);
        List<FinerGoods> finerGoodsList = warehouseGoodsDao.getGoodsPage(goodsPageDTO);
        log.debug("pageNum: {}, pageSize: {}, total: {}, pages:{}, finerGoodsList:{}", pageNum, pageSize, page.getTotal(), page.getPages(), finerGoodsList);
        PageUtil<FinerGoods> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(pageNum);
        pageUtil.setPageSize(pageSize);
        pageUtil.setTotal(page.getTotal());
        pageUtil.setPages(page.getPages());
        pageUtil.setData(finerGoodsList);
        return ResponseMessage.success(pageUtil);
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 新增商品
     * @date 2024/10/22 下午6:48
     * @Param finerGoods:
     **/
    public ResponseMessage addGoods(FinerGoods finerGoods) {
        if (StringUtils.isBlank(finerGoods.getProCode())) {
            return ResponseMessage.error("500", "商品编码不能为空");
        }
        //TODO: 校验商品编号是否重复
        if (StringUtils.isBlank(finerGoods.getProName())) {
            return ResponseMessage.error("500", "商品名称不能为空");
        }
        if (finerGoods.getPrice() == null) {
            return ResponseMessage.error("500", "商品价格不能为空");
        }
        if (finerGoods.getUnitId() == null) {
            return ResponseMessage.error("500", "商品单位不能为空");
        }
        if (finerGoods.getTypeId() == null) {
            return ResponseMessage.error("500", "商品类型不能为空");
        }
        if (finerGoods.getStockWarning() == null) {
            return ResponseMessage.error("500", "库存预警不能为空");
        }
        //TODO: 根据当前用户修改创建人
        finerGoods.setCreateBy(1);
        finerGoods.setCreateDate(new Date());
        finerGoods.setIsDel(0);
        int addNum = warehouseGoodsDao.addGoods(finerGoods);
        log.info("WarehouseGoodsServiceImpl addGoods addNum: {}", addNum);
        if (addNum > 0) {
            return ResponseMessage.success();
        }
        return ResponseMessage.error("500", "新增失败");
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 根据id查询商品
     * @date 2024/10/22 下午7:54
     * @Param id:
     **/
    public ResponseMessage getGoodsById(Integer id) {
        FinerGoods finerGoods = warehouseGoodsDao.getGoodsById(id);
        return ResponseMessage.success(finerGoods);
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 修改商品
     * @date 2024/10/22 下午7:55
     * @Param finerGoods:
     **/
    public ResponseMessage updateGoods(FinerGoods finerGoods) {
        if (finerGoods.getId() == null) {
            return ResponseMessage.error("500", "商品id不能为空");
        }
        if (StringUtils.isBlank(finerGoods.getProCode())) {
            return ResponseMessage.error("500", "商品编码不能为空");
        }
        //TODO: 校验商品编号是否重复
        if (StringUtils.isBlank(finerGoods.getProName())) {
            return ResponseMessage.error("500", "商品名称不能为空");
        }
        if (finerGoods.getPrice() == null) {
            return ResponseMessage.error("500", "商品价格不能为空");
        }
        if (finerGoods.getUnitId() == null) {
            return ResponseMessage.error("500", "商品单位不能为空");
        }
        if (finerGoods.getTypeId() == null) {
            return ResponseMessage.error("500", "商品类型不能为空");
        }
        if (finerGoods.getStockWarning() == null) {
            return ResponseMessage.error("500", "库存预警不能为空");
        }
        //TODO: 根据当前登录用户修改
        finerGoods.setModifyBy(1);
        finerGoods.setModifyDate(new Date());
        int updNum = warehouseGoodsDao.updateGoods(finerGoods);
        log.info("WarehouseGoodsServiceImpl updateGoods updNum: {}", updNum);
        if (updNum > 0) {
            return ResponseMessage.success();
        }
        return ResponseMessage.error("500", "修改失败");
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 删除商品
     * @date 2024/10/22 下午8:11
     * @Param id:
     **/
    public ResponseMessage deleteGoods(Integer id) {
        if (id == null) {
            return ResponseMessage.error("500", "商品id不能为空");
        }
        FinerGoods finerGoods = new FinerGoods();
        finerGoods.setId(id);
        finerGoods.setIsDel(1);
        finerGoods.setModifyBy(1);
        finerGoods.setModifyDate(new Date());
        int delNum = warehouseGoodsDao.updateGoods(finerGoods);
        log.info("WarehouseGoodsServiceImpl deleteGoods delNum: {}", delNum);
        if (delNum > 0) {
            return ResponseMessage.success();
        }
        return ResponseMessage.error("500", "删除失败");
    }
}
